/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmail.blackdog1987.carcare.dao.GasCouponsDao;
import com.gmail.blackdog1987.carcare.dao.ProductDao;
import com.gmail.blackdog1987.carcare.dao.PromotionDao;
import com.gmail.blackdog1987.carcare.dao.PromotionJoinDao;
import com.gmail.blackdog1987.carcare.dao.SmsDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TGasCoupons;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.entity.TPromotion;
import com.gmail.blackdog1987.carcare.entity.TPromotionJoin;
import com.gmail.blackdog1987.carcare.entity.TSms;
import com.gmail.blackdog1987.carcare.entity.base.Page;
import com.gmail.blackdog1987.carcare.service.PromotionService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;

/**
 * 
 * @author 董国超
 *
 */
@Service("promotionServiceImpl")
public class PromotionServiceImpl implements PromotionService {

	@Autowired
	private PromotionDao promotionDao;
	@Autowired
	private PromotionJoinDao promotionJoinDao;
	@Autowired
	private BaseJdbcDao baseJdbcDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private SmsDao smsDao;
	@Autowired
	private GasCouponsDao gasCouponsDao;
	
	@Override
	public Map<String, Object> getList(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String pageindex = filter.get("pageindex");
		String pagesize = filter.get("pagesize");
		int index = 1;
		int size = 999;
		if(pageindex!=null&&pageindex.length()!=0){
			index = Integer.valueOf(pageindex);
		}
		
		if(index<1){
			index = 1;
		}
		if(pagesize!=null&&pagesize.length()!=0){
			size = Integer.valueOf(pagesize);
		}
		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = "from t_promotion order by starttime desc";
		
		String limit = " limit " + (index-1)*size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" select count(*) " + sql , whereArgs.toArray());
		
		List<TPromotion> promotions = JdbcUtil.queryForList(baseJdbcDao, TPromotion.class, " select * " + sql + limit, whereArgs.toArray());
		
		Page page = new Page(promotions, index, totalCount, size);
		
		result.put("result", page);
		return result;
	}

	@Override
	public Map<String, Object> doApply(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String promotion_id = filter.get("promotion_id");
		String user_id = filter.get("user_id");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String idcard = filter.get("idcard");
		String driveage = filter.get("driveage");
		
		TPromotionJoin join = new TPromotionJoin();
		join.setDriveage(Integer.valueOf(driveage));
		join.setIdcard(idcard);
		join.setName(name);
		join.setPhone(phone);
		join.setPromotionId(Integer.valueOf(promotion_id));
		join.setUserId(Integer.valueOf(user_id));
		Serializable id = promotionJoinDao.create(join);
		join.setId((Integer) id);
		
		TPromotion promotion = promotionDao.get(Integer.valueOf(promotion_id));
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setPrice(promotion.getPrice());
		product.setSubject(promotion.getTitle());
		product.setTableName("t_promotion_join");
		product.setTableId((Integer) id);
		product.setUserid(Integer.valueOf(user_id));
		product.setBody("活动");
		product.setStatus(0);
		if(product.getPrice()<=0){		// 如果是0块钱，直接发短信下去，支付结果改为1
			product.setStatus(1);
			
			TGasCoupons coupons = new TGasCoupons();
			coupons.setCode(System.currentTimeMillis()+"");
			Calendar c =  Calendar.getInstance();
			coupons.setCreatetime(new Date());
			coupons.setLicense("");
			coupons.setStarttime(new Date());
			c.add(Calendar.YEAR, 1);
			coupons.setEndtime(new Date(c.getTime().getTime()));
			coupons.setStatus(0);
			coupons.setPrice(product.getPrice());
			coupons.setInfo(productDao.type.get("t_promotion_join")==null?"商品":productDao.type.get("t_promotion_join") +", 用户电话:"+phone);
			coupons.setUserid(Integer.valueOf(user_id));
			gasCouponsDao.save(coupons);
			
			// 发短信
			TSms sms = new TSms();
			sms.setContent("恭喜您下单成功，请携带本验证码到店使用:[" + coupons.getCode() + "]价值0元");
			if(smsDao.findBy("content", "恭喜您下单成功，请携带本验证码到店使用:[" + coupons.getCode() + "]价值0元").size()==0){
				sms.setCreatetime(new Date());
				sms.setFailuretimes(0);
				sms.setPhone(phone);
				sms.setUpdatetime(new Date());
				sms.setStatus(0);
				smsDao.save(sms);
			}
		}
		productDao.save(product);
		
		result.put("result", join);
		return result;
	}
}
